package com.web.web;

import java.math.BigDecimal;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.database.daos.IProjectDao;
import com.database.objects.*;
import com.load.ctrls.IDeveloperCtrl;
import com.metrics.ctrls.IGraphicCtrl;
import com.metrics.objects.*;
import com.opensymphony.xwork2.ActionSupport;

public class GraphicAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private IProjectDao projectDao;

	private IDeveloperCtrl developerCtrl;

	private IGraphicCtrl graphicCtrl;

	private Project project;

	private Metric metric;

	private Type type;

	private Status status;

	private Graphic graphic;

	private String start;

	private String finish;

	private BigDecimal percent;

	private boolean orderByName;

	private Time time;
	
	private String nameDeveloper;

	public String graphic() {
		try {
			project = projectDao.getByName(project.getName());
			List<Developer> developers = project.getDevelopers();
			Options options = new Options(graphic, metric, type, status, start, finish, time, percent, orderByName, nameDeveloper);

			developerCtrl.filterMaxDevelopersByPercent(developers, options);
			developerCtrl.filterDevelopersByName(developers, options);
			graphicCtrl.generateGraphic(ServletActionContext.getResponse().getOutputStream(), developers, options);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return NONE;
	}

	public void setGraphicCtrl(IGraphicCtrl graphicCtrl) {
		this.graphicCtrl = graphicCtrl;
	}

	public void setProjectDao(IProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public void setDeveloperCtrl(IDeveloperCtrl developerCtrl) {
		this.developerCtrl = developerCtrl;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public void setMetric(String metric) {
		this.metric = metric != null && !metric.equals("") ? Metric.valueOf(metric.replace(" ", "_")) : null;
	}

	public void setType(String type) {
		this.type = type != null && !type.equals("") ? Type.valueOf(type) : null;
	}

	public void setStatus(String status) {
		this.status = status != null && !status.equals("") ? Status.valueOf(status) : null;
	}

	public void setGraphic(String graphic) {
		this.graphic = graphic != null && !graphic.equals("") ? Graphic.valueOf(graphic.replace(" ", "_")) : null;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public void setFinish(String finish) {
		this.finish = finish;
	}

	public void setPercent(String percent) {
		this.percent = new BigDecimal(percent);
	}

	public void setOrderByName(boolean orderByName) {
		this.orderByName = orderByName;
	}

	public void setTime(String time) {
		this.time = Time.valueOf(time);
	}
	
	public void setNameDeveloper(String nameDeveloper) {
		this.nameDeveloper = nameDeveloper;
	}

}